﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NCT.Framework;
using System.Data.Common;
using NCT.Framework.Database;
using System.Data;

namespace VISS.API.Users {
    class UserImageBC : IUserImage {

        #region IUserImage Members

        public bool Insert(UserImageEnt item) {
            bool result = true;
            DbConnection dbCon = null;
            DbTransaction dbTrans = null;
            try {

                //Get connection name
                string connectionName = ConfigInfo.ConnectionName;

                //Create connection & transaction
                dbCon = Database.CreateConnection(connectionName, true);
                dbTrans = dbCon.BeginTransaction();

                item.DateCreated = DateTime.Now;

                // Insert UserImage to DB
                result = UserImageDA.Insert(item, connectionName, dbCon, dbTrans) > 0;

                // Increase ImageCount in UserGeneralProfile
                int imageCount;
                result = UserGeneralProfileDA.IncreaseImageCount(item.UserKey, 1, out imageCount, connectionName, dbCon, dbTrans) > 0;

                //Add log
                if (result) {
                }

                // Insert UserImage into Cache
                if (result && ConfigInfo.UseCache) {

                    if (result) {
                    }
                }
            }
            catch (Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserImageBC.Insert({0}): \n" + ex.ToString(),
                                    SerializerUtils.JsonSerialize(item)));
                result = false;
            }
            finally {
                if (dbTrans != null) {
                    if (result) {
                        dbTrans.Commit();
                    }
                    else {
                        // rollback
                        dbTrans.Rollback();
                    }
                }
                if (dbCon != null && dbCon.State == ConnectionState.Open) {
                    dbCon.Close();
                    
                }
            }
            return result;
        }

        public bool Delete(long iD, string userKey) {
            bool result = true;
            DbConnection dbCon = null;
            DbTransaction dbTrans = null;
            try {

                //Get connection name
                string connectionName = ConfigInfo.ConnectionName;

                //Create connection & transaction
                dbCon = Database.CreateConnection(connectionName, true);
                dbTrans = dbCon.BeginTransaction();

                // Delete UserImage from DB
                result = UserImageDA.Delete(userKey, iD, connectionName, dbCon, dbTrans) > 0;

                //Decrease ImageCount in UserGeneralProfile
                int imageCount;
                if (result) {
                    result = UserGeneralProfileDA.DecreaseImageCount(userKey, 1, out imageCount, connectionName, dbCon, dbTrans) > 0;
                }                

                //Add log
                if (result) {
                }

                // Delete UserImage from Cache
                if (result && ConfigInfo.UseCache) {

                    if (result) {
                    }
                }
            }
            catch (Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserImageBC.Delete({0}, {1}): \n" + ex.ToString(),
                                    iD, userKey));
                result = false;
            }
            finally {
                if (dbTrans != null) {
                    if (result) {
                        dbTrans.Commit();
                    }
                    else {
                        // rollback
                        dbTrans.Rollback();
                    }
                }
                if (dbCon != null && dbCon.State == ConnectionState.Open) {
                    dbCon.Close();
                    
                }
            }

            return result;
        }

        public List<UserImageEnt> GetByUserKey(string userKey) {
            List<UserImageEnt> result = new List<UserImageEnt>();
            try {
                if (ConfigInfo.UseCache) {
                }
                else {
                    //Get connection name
                    string connectionName = ConfigInfo.ConnectionName;

                    result = UserImageDA.GetByUserKey(userKey, connectionName, null, null);
                }
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserImageBC.GetByUserKey({0}): \n" + ex.ToString(),
                                    userKey));
            }
            return result;
        }

        public UserImageEnt GetByUserKeyAndID(string userKey, long iD) {
            UserImageEnt result = null;
            try {
                if (ConfigInfo.UseCache) {
                }
                else {
                    //Get connection name
                    string connectionName = ConfigInfo.ConnectionName;

                    result = UserImageDA.GetByUserKeyAndID(userKey, iD, connectionName, null, null);
                }
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserImageBC.GetByUserKey({0}, {1}{0}): \n" + ex.ToString(),
                                   userKey, iD));
            }
            return result;
        }

        public List<UserImageEnt> GetByUserKeyWithPaging(string userKey, int page, int recordPerPage, out int totalRecord) {
            List<UserImageEnt> result = new List<UserImageEnt>();
            totalRecord = 0;
            try {
                if (ConfigInfo.UseCache) {
                }
                else {
                    //Get connection name
                    string connectionName = ConfigInfo.ConnectionName;

                    totalRecord = UserGeneralProfileDA.GetImageCountByUserKey(userKey, connectionName, null, null);
                    if (totalRecord == 0) {
                        return result;
                    }

                    int totalPage = totalRecord / recordPerPage;
                    if (totalRecord % recordPerPage != 0) {
                        totalPage++;
                    }
                    if (page > totalPage) {
                        return result;
                    }

                    int startRow = (page - 1) * recordPerPage + 1;
                    int endRow = startRow + recordPerPage - 1;

                    if (endRow > totalRecord) {
                        endRow = totalRecord;
                    }

                    result = UserImageDA.GetByUserKeyWithPaging(userKey, startRow, endRow, connectionName, null, null);
                }

                return result;
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserImageBC.GetByUserKeyWithPaging({0}, {1}, {2}): \n" + ex.ToString(),
                                    userKey, page, recordPerPage));
                totalRecord = -1;
            }
            return result;
        }

        public int GetCountByUserKey(string userKey) {
            int result = 0;
            try {
                if (ConfigInfo.UseCache) {
                }
                else {
                    //Get connection name
                    string connectionName = ConfigInfo.ConnectionName;

                    result = UserGeneralProfileDA.GetImageCountByUserKey(userKey, connectionName, null, null);
                }
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserImageBC.GetCountByUserKey({0}): \n" + ex.ToString(), userKey));
                result = 0;
            }
            return result;
        }

        #endregion
    }
}
